[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
User-Defined functions are defined with the pattern keyword:
function
To define a function to find the maximum of two arguments:
function max(a1,a2) {
return a1 > a2 ? a1 : a2;
}
The function would return either numeric if both arguments are numeric.
If either argument is a string, then the function would return the
string highest in the ASCII collating sequence.
A function may be defined with no arguments. To define a function to
find the maximum value in the current input record:
function max_input() {
local i;
local max = $1;
for ( i = 2 ; i <= NF ; i++ ) if ( max < $i ) max = $i;
return max;
}
This example is notable in the use of 'local' variables in the function
definition. The 'local' variable 'i' and 'max' disappear upon exit
from the function.
See Also:
Invoking
Variable
_arg_chk
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson